package com.example.myapplication

import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplcation.fragment.BaseActivity
import com.example.myapplication.adapter.PopularScienceMineAdapter
import com.example.myapplication.database.DataBaseUtils
import com.example.total.databinding.ActivityPopularScienceBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class PopularScienceMineActivity : BaseActivity<ActivityPopularScienceBinding>() {
    override fun initData() {
        binding.back.setOnClickListener { finish() }
        binding.recycler.layoutManager = LinearLayoutManager(this)
        val adapter = PopularScienceMineAdapter()
        binding.recycler.adapter = adapter
        GlobalScope.launch(Dispatchers.IO){
            val data = DataBaseUtils.getInstance(this@PopularScienceMineActivity).dataBaseDao().queryAllPopularScience()
            withContext(Dispatchers.Main){
                adapter.submitList(data)
                adapter.setOnItemClickListener{ _,_,pos->
                    adapter.getItem(pos)?.run {
                        Intent().apply {
                            setClass(this@PopularScienceMineActivity, PopularScienceDetailsMineActivity::class.java)
                            putExtra("id",id)
                            startActivity(this)
                        }
                    }

                }
            }
        }


    }
}
